<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\DB;

class RoleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'=>(integer)$this->id,
            'display_name'=>(string)$this->display_name,
            'description'=>(string)$this->description,
            'prmissions'=>$this->getPermissionAttribute(),
        ];
    }
    public function getPermissionAttribute()
    {
        $result = DB::table('permission_role')
            ->join('permissions', 'permission_role.permission_id', '=', 'permissions.id')
            ->where(['permission_role.role_id'=>$this->id])->pluck('permissions.id')->toArray();
        return $result;
    }
}
